package com.qnw.CardGroupManagement;


import android.content.res.*;
import android.os.*;
import android.support.v4.app.*;
import android.support.v4.view.*;
import android.support.v4.widget.*;
import android.support.v7.widget.*;
import android.view.*;
import com.ygo.feihua.*;

import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.*;
import com.qnw.CardGroupManagement.fragment.*;
import com.ygo.feihua.fragment.*;

public class J5 extends AppCompatActivity 
{
	private ActionBarDrawerToggle mDrawerToggle;
	private DrawerLayout mDrawerlayout;
	private FragmentManager fragmentman;
	private FragmentImage fragmentimage;
	private FragmentImageList fragmentimagelist;
	public Gongju gj;

	public void setkt(int tYPE_PATH, String s)
	{
		fragmentimage.setKt(tYPE_PATH,s);
		
		// TODO: Implement this method
	}
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
	{
        super.onCreate(savedInstanceState);
        setContentView(R.layout.j5_layout);


		Toolbar tool=(Toolbar) findViewById(R.id.toolbar);
		gj=Gongju.getdx(this);

		gj.cshToolbar(tool,"卡图管理");


		tool.setOnMenuItemClickListener(onMenuItemClick);

		mDrawerlayout=(DrawerLayout) findViewById(R.id.j5_drawerlayout);

		fragmentman=getSupportFragmentManager();
		fragmentimagelist=(FragmentImageList) fragmentman.findFragmentById(R.id.j5_menu_left);

		fragmentimage=new FragmentImage();
		mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerlayout, tool, R.string.drawer_open, R.string.drawer_close);

		//设置侧滑栏开启/关闭监听
		mDrawerlayout.setDrawerListener(new MyDrawerListener());

		fragmentman.beginTransaction().add(R.id.j5_flContent, fragmentimage).commit();


	}


	private Toolbar.OnMenuItemClickListener onMenuItemClick = new Toolbar.OnMenuItemClickListener() {

		@Override
		public boolean onMenuItemClick(MenuItem p1)
		{
			switch(p1.getItemId()){
				case R.id.action_qd:				
					fragmentimage.save();
					break;
			}
			// TODO: Implement this method
			return true;
		}
	};




	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{

		getMenuInflater().inflate(R.menu.menu_qd, menu);

		// TODO: Implement this method
		return super.onCreateOptionsMenu(menu);
	}



	public void closeDrawer(){
		if (mDrawerlayout != null) {
			mDrawerlayout.closeDrawer(GravityCompat.START);
		}
	}
	/** activity创建完成后 */
    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
		/*   该方法会自动和actionBar关联, 将开关的图片显示在了action上，
		 如果不设置，也可以有抽屉的效果，不过是默认的图标 没有的话进去的默认是个箭头。。
		 正常应该是三横杠的
		 */ 
		mDrawerToggle.syncState();

    }
	/** 菜单键点击的事件处理 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		return mDrawerToggle.onOptionsItemSelected(item) || super.onOptionsItemSelected(item);
	}


	/** 设备配置改变时 */
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
		mDrawerToggle.onConfigurationChanged(newConfig);
    }

	/** drawer的监听 */
	private class MyDrawerListener implements DrawerLayout.DrawerListener {
		@Override
		public void onDrawerOpened(View drawerView) {
			// 打开drawer
			mDrawerToggle.onDrawerOpened(drawerView);//开关状态改为opened
			drawerView.setClickable(true);
		}

		@Override
		public void onDrawerClosed(View drawerView) {
			// 关闭drawer
			mDrawerToggle.onDrawerClosed(drawerView);//开关状态改为closed

		}

		@Override
		public void onDrawerSlide(View drawerView, float slideOffset) {// drawer滑动的回调
			mDrawerToggle.onDrawerSlide(drawerView, slideOffset);
		}

		@Override
		public void onDrawerStateChanged(int newState) {// drawer状态改变的回调
			mDrawerToggle.onDrawerStateChanged(newState);
		}
	}


	protected boolean isNavDrawerOpen() {
        return mDrawerlayout != null && mDrawerlayout.isDrawerOpen(GravityCompat.START);
    }

	@Override
	public void onBackPressed()
	{
		// TODO: Implement this method
		
		if(fragmentimagelist.isSelected()){
			fragmentimagelist.setselected(false);
		}else{
			if(isNavDrawerOpen()){
				closeDrawer();
			}else{

				finish();
			}
		}
		
	}
	
	
	
	
}
